﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using System.Data.SQLite;
using System.Xml;

using PDAMain.Entity;

namespace PDAMain.Forms.CustomerWork
{
    public partial class CustomerList : Form
    {
        public CustomerList()
        {
            InitializeComponent();
        }

        //獲取客服工单数据
        private void RefreshData(DataTable dt)
        {
            if (Identity.customerFlag == false)
                Identity.customerFlag = !Identity.customerFlag;
            if (dt != null && dt.Rows.Count > 0)
            {
                WorkList.DataSource = dt;
                DataGridTableStyle dts = new DataGridTableStyle();
                dts.MappingName = dt.TableName;

                DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();
                col1.HeaderText = "活动编号";
                col1.MappingName = "CWOrder_No";
                col1.Width = 84;
                dts.GridColumnStyles.Add(col1);

                DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
                col2.HeaderText = "地址";
                col2.MappingName = "Address";
                col2.Width = 170;
                dts.GridColumnStyles.Add(col2);

                DataGridTextBoxColumn col3 = new DataGridTextBoxColumn();
                col3.HeaderText = "优先级";
                col3.MappingName = "Event_Level";
                col3.Width = 60;
                dts.GridColumnStyles.Add(col3);

                /*DataGridTextBoxColumn col4 = new DataGridTextBoxColumn();
                col4.HeaderText = "状态";
                col4.MappingName = "STATUS";
                col4.Width = 60;
                dts.GridColumnStyles.Add(col4);*/

                WorkList.TableStyles.Clear();
                WorkList.TableStyles.Add(dts);
                WorkList.Refresh();
            }
        }

        private void CustomerList_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            try
            {
                //验证数据库是否存在
                DataSet dbCheck = DataHelper.CustomerWorkDao.CustomerList();
                if (dbCheck.Tables[0].Rows.Count > -1)
                    DataHelper.CustomerWorkDao.updateDataFromWebservice();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                dt = DataHelper.CustomerWorkDao.list().Tables[0];
                RefreshData(dt);
            }
        }

        private void WorkList_Click(object sender, EventArgs e)
        {
            if (WorkList.CurrentRowIndex == -1)
                return;
            //獲取單號
            string strListID = ((DataTable)WorkList.DataSource).Rows[WorkList.CurrentRowIndex][0].ToString();
            Identity.selectedWorkOrderID = strListID;

            CustomerDetail Detail = new CustomerDetail();
            Detail.customerID = strListID;
            Detail.Show();
            this.Close();
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            //跳轉簡單查詢
            CustomerSelect select = new CustomerSelect();
            select.Show();
            this.Close();
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            //跳轉高級查詢
            CustomerComplexSel complexSel = new CustomerComplexSel();
            complexSel.Show();
            this.Close();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //返回
            FuntionView fun = new FuntionView();
            fun.Show();
            this.Close();
        }

        private void deleteData()
        {
            if (Identity.customerFlag)
                return;
            if (MessageBox.Show("请确认重载数据", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                DataHelper.CustomerWorkDao.CustomerDrop();
                Identity.customerFlag = !Identity.customerFlag;
            }
            
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            //同步數據
            //從Webservice同步數據
            try
            {
                DataHelper.CustomerWorkDao.updateDataFromWebservice();
                MessageBox.Show("同步完成");
            }
            catch { MessageBox.Show("网络未连接"); }
            
            DataTable dt = new DataTable();   
            dt = DataHelper.CustomerWorkDao.list().Tables[0];
            RefreshData(dt);
        }

        private void CustomerList_Closed(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                DataHelper.CustomerWorkDao.updateDataFromWebservice();
                DataTable dt = DataHelper.CustomerWorkDao.list().Tables[0];
                RefreshData(dt);
            }
            catch (Exception ex)
            {
            }
        }
    }
}